package com.ycjy.szteacher.controller;


import com.ycjy.szteacher.entity.Teacher;
import com.ycjy.szteacher.mapper.TeacherMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘洋
 * @since 2020-10-08
 */
@SuppressWarnings("ALL")
@Controller
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private TeacherMapper teacherMapper;
    @RequestMapping("/login")
    public String login(Teacher teacher, HttpServletRequest request){
        // 根据用户名和密码创建 Token
        UsernamePasswordToken token = new UsernamePasswordToken(teacher.getTeacherName(), teacher.getTeacherPassword());
        // 获取 subject 认证主体
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            try {
                // 开始认证，这一步会跳到我们自定义的 Realm 中
                subject.login(token);
                // 剔除其他此账号在其它地方登录
                request.getSession().setAttribute("teacher", teacher);
                return "/index";
                //subject.checkRole("admin");
                //int reuslt = iTeacherService.insertLoginRecord("登陆成功！！", user.getUserName());
            } catch (Exception e) {
                return "/page/login";
            }
        } else {
            return "/index";
        }

    }
    @RequestMapping("/logout")
    public String logout(HttpServletResponse response) {
        Subject lvSubject = SecurityUtils.getSubject();
        lvSubject.logout();
        return "/page/login";
    }

}

